import 'package:flutter/material.dart';

class DottedLineWidget extends StatelessWidget {
  final double dashWidth; // 虚线的宽度
  final double dashHeight; // 虚线的高度
  final double dashSpace; // 虚线之间的间隔
  final Color color; // 虚线的颜色
  final Axis direction; // 虚线的方向，水平或垂直

  const DottedLineWidget({
    super.key,
    this.dashWidth = 6.0,
    this.dashHeight = 2.0,
    this.dashSpace = 4.0,
    this.color = Colors.black,
    this.direction = Axis.horizontal,
  });

  @override
  Widget build(BuildContext context) {
    return CustomPaint(
      size: direction == Axis.horizontal
          ? Size(double.infinity, dashHeight)
          : Size(dashWidth, double.infinity),
      painter: DottedLinePainter(
        dashWidth: dashWidth,
        dashHeight: dashHeight,
        dashSpace: dashSpace,
        color: color,
        direction: direction,
      ),
    );
  }
}

class DottedLinePainter extends CustomPainter {
  final double dashWidth;
  final double dashHeight;
  final double dashSpace;
  final Color color;
  final Axis direction;

  DottedLinePainter({
    required this.dashWidth,
    required this.dashHeight,
    required this.dashSpace,
    required this.color,
    required this.direction,
  });

  @override
  void paint(Canvas canvas, Size size) {
    final paint = Paint()
      ..color = color
      ..strokeCap = StrokeCap.round
      ..strokeWidth = direction == Axis.horizontal ? dashHeight : dashWidth;

    double start = 0.0;
    if (direction == Axis.horizontal) {
      while (start < size.width) {
        canvas.drawLine(
            Offset(start, 0),
            Offset(start + dashWidth, 0),
            paint);
        start += dashWidth + dashSpace;
      }
    } else {
      while (start < size.height) {
        canvas.drawLine(
            Offset(0, start),
            Offset(0, start + dashHeight),
            paint);
        start += dashHeight + dashSpace;
      }
    }
  }

  @override
  bool shouldRepaint(CustomPainter oldDelegate) {
    return false;
  }
}